<?php

class SiteController extends Controller
{

    private $_model;

    /**
     * Declares class-based actions.
     */
    public function actions()
    {
        return array(
            // page action renders "static" pages stored under 'protected/views/site/pages'
            // They can be accessed via: index.php?r=site/page&view=FileName
            'page' => array(
                'class' => 'CViewAction',
            ),
        );
    }

    /**
     * This is the default 'index' action that is invoked
     * when an action is not explicitly requested by users.
     */
    public function actionIndex()
    {
        $this->render('index');
    }

    /**
     * This is the default 'index' action that is invoked
     * when an action is not explicitly requested by users.
     */
    public function actionLanguage()
    {
        Yii::app()->language = $_GET['language'];
        $this->render('index');
    }

    /**
     * This is the default 'index' action that is invoked
     * when an action is not explicitly requested by users.
     */
    public function actionDetails()
    {
        $player = $this->loadModel();
        $samePlayers = Player::model()->findAll(array(
            'condition' => 'position=:position',
            'params' => array(':position' => $player->position)
        ));
        $this->render('details', array(
            'model' => $player,
            'samePlayers' => $samePlayers
        ));
    }

    public function actionSearch()
    {
        $criteria = new CDbCriteria(array(
            'order' => 'id ASC'
        ));
        if (isset($_POST['position'])) {
            $criteria->addSearchCondition('position', $_POST['position']);
        }

        if (isset($_POST['sex'])) {
            $criteria->addSearchCondition('person.sex', $_POST['sex']);
        }

        if (isset($_POST['age'])) {
            $criteria->addSearchCondition('person.age', $_POST['age']);
        }

        if (isset($_POST['citizenship'])) {
            $criteria->addSearchCondition('person.citizenship', $_POST['citizenship']);
        }

        if ($_POST['profile'] == 'Player') {
            $type = 'players';
            $model = new Player;
        } else {
            $type = 'coaches';
            $model = new Coach;
        }

        $total = $model->count($criteria);
        $pages = new CPagination($total);
        $pages->pageSize = Yii::app()->params['recordsPerPage'];
        $pages->applyLimit($criteria);
        $list = $model->findAll($criteria);
        $this->render('search', array(
            'list' => $list,
            'pages' => $pages,
            'type' => $type
        ));
    }

    public function actionContact()
    {
        $email = $_POST['email'];
        $subject = $_POST['subject'];
        $message = $_POST['message'];
        $copy = $_POST['copy'];
        $params = array();

        $this->sendMail($message, $subject, Yii::app()->params['admin.email'], $params);
        if ($copy) {
            $this->sendMail($message, $subject, $email, $params);
        }
    }

    /**
     * This is the action to handle external exceptions.
     */
    public function actionError()
    {
        if ($error = Yii::app()->errorHandler->error) {
            if (Yii::app()->request->isAjaxRequest) {
                echo $error['message'];
            } else {
                $this->render('error', $error);
            }
        }
    }

    public function getBodyClass()
    {
        if ($this->action->id === 'index' || $this->action->id === 'language') {
            return '';
        } else {
            return 'class="inner"';
        }
    }

    public function getLastEnrolledPlayers()
    {
        return Player::model()->findAll(array(
            'order' => 'id DESC',
            'limit' => 4,
        ));
    }

    public function loadModel()
    {
        if ($this->_model === null) {
            if (isset($_GET['id'])) {
                $this->_model = Player::model()->findByPk($_GET['id']);
            }
            if ($this->_model === null) {
                throw new CHttpException(404, 'The requested page does not exist.');
            }
        }
        return $this->_model;
    }
}